package cs2114.antiFingerMines;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import java.util.Observable;
import java.util.Observer;

/**
 * // -------------------------------------------------------------------------
 * /** The View for the AntiFingerMines game.
 *
 * @author Blake Whaley
 * @author Graham McLaughlin (gjm1324)
 * @author William Vuong (wnvuong)
 * @version 2012.29.4
 */
public class AntiFingerMinesView
    extends View
{
    private MineField field;
    private boolean visible;


    /**
     * Constructor for the View.
     *
     * @param context
     *            the context.
     * @param attrs
     *            the attribute set.
     */
    public AntiFingerMinesView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }


    /**
     * This lets the view know if the field should be drawn or not
     * @param status represents true if it is to be drawn false if it isn't
     */
    public void setVisible(boolean status)
    {
        visible = status;
    }

    /**
     * What do we want here? I think the position should be represented by a
     * dot.
     *
     * @canvas the canvas the view is drawn on.
     */
    public void onDraw(Canvas canvas)
    {
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        Paint text = new Paint();
        text.setColor(Color.BLACK);
        if (visible)
        {
            for (int i = 0; i < field.size(); i++)
            {

    	            canvas.drawCircle(
    	                field.getMine(i).getX(),
    	                field.getMine(i).getY(),
    	                field.getMine(i).getRadius(),
    	                paint);
            }
        }

        Paint green = new Paint();
        green.setColor(Color.GREEN);
        green.setStyle(Paint.Style.FILL);

        paint.setColor(Color.CYAN);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(
            0,
            field.getEndOfField(),
            getWidth(),
            getHeight(),
            green);
        canvas.drawCircle(field.getFingerLocation().getXloc(), field
            .getFingerLocation().getYloc(), 20, paint);
    }

    /**
     * This method handles whenever the screen is touched
     * @returns boolean true if handled, false if not
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        boolean handled = false;
        if (event.getAction() == MotionEvent.ACTION_DOWN
            && event.getX() > field.getFingerLocation().getXloc() - 15
            && event.getX() < field.getFingerLocation().getXloc() + 15
            && event.getY() > field.getFingerLocation().getYloc() - 15
            && event.getY() < field.getFingerLocation().getYloc() + 15)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        return handled;
    }


    /**
     * Sets the model.
     *
     * @param field
     */
    public void setModel(MineField field)
    {
        this.field = field;
        this.field.addObserver(new MineObserver());
    }

    /**
     * An observer that listens for changes made to the MineField model.
     * This is a nested class inside the view so that it can still access
     * methods that belong to the surrounding view.
     */
    private class MineObserver
        implements Observer
    {
        /**
         * Called when the MineFieldmodel is changed (for example, when the
         * user drags a finger around the view).
         *
         * @param observable the Observable object that was changed
         * @param data extra data about the notification; unused here
         */
        public void update(Observable observable, Object data)
        {
            invalidate();
        }
    }

}
